package naga.x.platform2.channel.sina.support;

import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import naga.x.platform2.utils.CommonUtils;

/**
 * @see <a href=
 *      "http://git.oschina.net/sinagamesdk/sinagamesdk-h5/blob/master/%E6%96%B0%E6%B5%AA%E6%B8%B8%E6%88%8FH5%E6%8E%A5%E5%85%A5%E6%96%87%E6%A1%A3.md">
 *      新浪游戏H5</a>
 * @author yang.li
 *
 */
public class SinaApi {

	public static String makeSign(Map<String, String> map, String key) {
		Map<String, String> tree = map instanceof TreeMap ? map : new TreeMap<String, String>(map);
		StringBuilder buf = new StringBuilder(128);
		for (Entry<String, String> entry : tree.entrySet()) {
			if (!"signature".equals(entry.getKey())) {
				buf.append(entry.getKey()).append('=').append(entry.getValue()).append('&');
			}
		}
		buf.setLength(buf.length() - 1); // remove the last '&'
		buf.append('|').append(key);
		return CommonUtils.md5(buf.toString());
	}
	
	public static String makeSignOfPay(Map<String, String> map, String key) {
		Map<String, String> tree = map instanceof TreeMap ? map : new TreeMap<String, String>(map);
		StringBuilder buf = new StringBuilder(128);
		for (Entry<String, String> entry : tree.entrySet()) {
			if (!"signature".equals(entry.getKey())) {
				buf.append(entry.getKey()).append('|').append(entry.getValue()).append('|');
			}
		}
		buf.append(key);
		return CommonUtils.encryptToHexString("SHA-1", buf.toString());
	}
	
}
